//
//  NewsViewController.m
//  Crocodile
//
//  Created by tin vu on 8/29/12.
//  Copyright (c) 2012 hust. All rights reserved.
//

#import "NewsViewController.h"
#import "News.h"
#import "Constant.h"

@interface NewsViewController ()

@end

@implementation NewsViewController
@synthesize newsTableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createData];
       
    }
    return self;
}

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
	// Do any additional setup after loading the view.
    // Background image
//    UIImage *backgroundImg = [UIImage imageNamed:kImgBackground];
//    UIImageView *backgroundImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
//    [backgroundImgView setImage:backgroundImg];
//    backgroundImgView.alpha = 1;
//    [self.view addSubview:backgroundImgView];
//    [backgroundImg release];
//    [backgroundImgView release];
    
    
    self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kImgBackground]];
    self.parentViewController.view.alpha = 1.00;
    
    // Header Image
    UIImage *headerImg = [UIImage imageNamed:kImgHeader];
    UIImageView *headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [headerImgView setImage:headerImg];
    [self.view addSubview:headerImgView];
    [headerImg release];
    [headerImgView release];
    
    // Title
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 170, 34)];
    titleLbl.backgroundColor = [UIColor clearColor];
    titleLbl.center = CGPointMake(160, 20);
    titleLbl.textAlignment = UITextAlignmentCenter;
    titleLbl.textColor = [UIColor greenColor];
    titleLbl.font = [UIFont boldSystemFontOfSize:20];
    titleLbl.text = kStrNewsTabTitle;
    [self.view addSubview:titleLbl];
    [titleLbl release];
    
     [self loadTable];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Create Data and Table

-(void) createData
{
    dataArray = [[NSMutableArray alloc]init];
    for(int i = 0; i <10; i++)
    {
        News *news = [[News alloc]initWithName:1 title:@"test" imageUrl:@"qnga1.jpeg" content:@"test" date:@"06-09-2012"];
        [dataArray addObject:news];
    }
}

-(void) loadTable
{
    newsTableView = [[UITableView alloc]initWithFrame:CGRectMake(5, 50, 310, 400) style:UITableViewStylePlain];
    newsTableView.backgroundColor = [UIColor clearColor];
    newsTableView.separatorColor = [UIColor clearColor];
    newsTableView.delegate = self;
    newsTableView.dataSource =self;
    [self.view addSubview:newsTableView];
    
}

#pragma mark - UITableView Delegates & DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = nil;
    UIImageView *newsImageView;
    UILabel *titleLabel, *timeLabel;
    News *news = [dataArray objectAtIndex:indexPath.row];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kImgCellBg]];
        bgView.alpha = 0.25;
        cell.backgroundView = bgView;
   
        newsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        newsImageView.backgroundColor = [UIColor clearColor];
        newsImageView.tag = 1000;
        [cell addSubview:newsImageView];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, 300, 20)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor blueColor];
        titleLabel.tag = 1001;
        [cell addSubview:titleLabel];
        
        timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 25, 300, 20)];
        timeLabel.backgroundColor = [UIColor clearColor];
        timeLabel.tag = 1002;
        [cell addSubview:timeLabel];
        
    }
    
    newsImageView = (UIImageView*)[cell viewWithTag:1000];
    titleLabel = (UILabel *)[cell viewWithTag:1001];
    timeLabel = (UILabel*)[cell viewWithTag:1002];
    
    [newsImageView setImage:[UIImage imageNamed:news.imageUrl]];
    titleLabel.text = news.title;
    timeLabel.text = news.date;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - Action
@end
